/**
 * Entry for a String field
 * 
 * @author patrick.zulian@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.itemeditor.fieldselector;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import ch.unisi.inf.projects.java.uom.Constants;
import ch.unisi.inf.projects.java.uom.elements.Item;

public final class StringEntryGUI extends EntryGUI{
	
	private JLabel label;
	private JTextField textField;
	
	public StringEntryGUI(final String key, final String value, final JPanel containerPanel, final Item item)
	{
		super(key, value, item);
		String displayString = "";
		textField= new JTextField(value);
		final Dimension size = new Dimension(150,50);
		if (key == null)
			label = new JLabel(displayString);
		else {
			String[] subStrings =  key.split(Constants.ITEM_PARSECHAR.value());
		
			if(subStrings.length > 1) {
				for(int i = 1; i< subStrings.length; i++){
					displayString += subStrings[i] + " ";
				}
			}
			else {
				displayString = key;
			}
			label = new JLabel(displayString + ": ");
			label.setHorizontalAlignment(JLabel.RIGHT);
			textField.setToolTipText("<html> <b>" + displayString + ":</b>" + "<br>" + value + "</html>");
			label.setPreferredSize(size);
			containerPanel.add(label);
		}
			textField.setBorder(new EmptyBorder(0,0,0,0));
			textField.setMinimumSize(size);
			textField.setPreferredSize(size);
			textField.setCaretPosition(0);
			containerPanel.add(textField);
	}
	
	public boolean setFromInput()
	{
		String newValue = textField.getText();
		if((value == null) || (!value.equals(newValue)))
		{	
			if(newValue.matches("[A-Za-z0-9.,/@ ]*")) {
				modified = true;
				value = newValue;
				return true;
			}
			else
				errorMessage = newValue;
			return false;
		}
		return true;
		
	}
	
	
	@Override
	public void select() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void refresh() {
		if(modified){
		//setFromInput();
		//save();
		modified = false;
		}
	}

}	
